package cn.acmenms.controller;

import cn.acmenms.annotation.IgnoreSecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * @ClassName: AppController
 * @Description: 用于获取应用的配置信息
 * @author: 神奇的鸭鸭
 * @Date: 2018/2/2 13:34
 */
@Deprecated
/*@RestController*/
public class AppController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(AppController.class);

    /**
     * 获取应用初始化数据 不需要token验证
     *
     * @return 初始化数据
     */
    @IgnoreSecurity
    @RequestMapping(value = "/initApp", method = RequestMethod.GET)
    public String initApp(HttpServletResponse response) {
        response.setContentType("text/json");
        response.setCharacterEncoding("UTF-8");
        String appInfo = "";
        try {
            appInfo = this.getAppInfo();
        } catch (Exception e) {
            logger.error("获取应用信息失败！", e);
        }
        return appInfo;
    }

    /**
     * 获取应用信息
     *
     * @return 应用信息
     */
    private String getAppInfo() throws Exception {
        // 获取JSON文件
        ClassLoader classLoader = getClass().getClassLoader();
        URL url = classLoader.getResource("app-data.json");
        String filePath = "";
        if (null != (url != null ? url.getPath() : null)) {
            filePath = url.getPath();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        StringBuilder content = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            content.append(line);
        }
        return content.toString();
    }
}